package com.bugman.managed;

import com.bugman.business.ProjectService;
import com.bugman.entity.Project;
import com.bugman.entity.User;
import com.bugman.exceptions.ProjectAlreadyExistException;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author Ediczech
 */
public class ProjectBean extends BaseBean {
    
    private String projectId;
    private String username;
    private Project project;
    private ResourceBundle rb = getBundle();
    private List<String> projectDeveloperIds = new ArrayList<String>();
    private List<SelectItem> developers = new ArrayList<SelectItem>();
    private List<User> projectDevelopers = new ArrayList<User>();
    @EJB
    ProjectService projectService;
    

    @PostConstruct
    public void init() {
        // create List of selectItems
        for (User developer : userService.getDevelopers(getLoggedUser())) {
            developers.add(new SelectItem(developer.getId().toString(), developer.getNick()));
        }

        projectId = getRequestParameter("id");
        if (projectId != null) {
            project = projectService.getProject(Integer.valueOf(projectId));
            logger.info("Nacten projekt z requestu: " + project);
            for (User pd : project.getDevelopers()) {
                projectDeveloperIds.add(pd.getId().toString());
            }
        } else {
            project = new Project();
        }
    }

    public boolean isProjectExist() {
        if (project.getId() != null) {
            return true;
        } else {
            return false;
        }
    }

    public String create() {
        projectDevelopers = userService.getUsers(toInteger(projectDeveloperIds));
        project.setCreator(getLoggedUser());
        project.setDevelopers(projectDevelopers);
        try {
        projectService.createProject(project);
        } catch (ProjectAlreadyExistException ex) {
            logger.error("Projekt se jmenem: " + project.getName() + "jiz existuje.");
            return "fail";
        }
        logger.info("Vytvarim project " + project);
        return "success";
    }

    public String edit() {
        projectDevelopers = userService.getUsers(toInteger(projectDeveloperIds));
        project.setDevelopers(projectDevelopers);
        projectService.updateProject(project);
        logger.info("Menim project " + project);
        return "success";
    }

    public void projectNameValidator(FacesContext context, UIComponent component,
            Object object) throws ValidatorException {

        username = (String) object;

        if (projectService.projectExist(username)) {
            throw new ValidatorException(new FacesMessage(
                    FacesMessage.SEVERITY_ERROR,
                    rb.getString("createProject_error_projectExist"),
                    rb.getString("createProject_error_projectExist_detail")));
        }
    }

    private List<Integer> toInteger(List<String> list) {
        List<Integer> result = new ArrayList<Integer>();
        for (String item : list) {
            result.add(Integer.valueOf(item));
        }
        return result;
    }

    public List<SelectItem> getDevelopers() {
        return developers;
    }

    public void setDevelopers(List<SelectItem> developers) {
        this.developers = developers;
    }

    public List<String> getProjectDevelopers() {
        return projectDeveloperIds;
    }

    public void setProjectDevelopers(List<String> projectDevelopers) {
        this.projectDeveloperIds = projectDevelopers;
    }

    public Project getProject() {
        return project;
    }

    public void setProject(Project project) {
        this.project = project;
    }
}
